package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.domain.SoulTest.*;
import com.tanhua.dubbo.api.SoulTest.SoulTestApi;
import com.tanhua.dubbo.mapper.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author 钱柏羽
 * api实现
 */
@Service
public class SoulTestImpl implements SoulTestApi {
    @Autowired
    private OptionMapper optionMapper;
    @Autowired
    private SoulQuestionMapper soulQuestionMapper;
    @Autowired
    private SoulTestMapper soulTestMapper;
    @Autowired
    private SoulScoreMapper soulScoreMapper;
    @Autowired
    private ResultMapper resultMapper;
    @Override
    public void saveUserScore(Long userId, Integer surveyScore, String facility) {
        soulScoreMapper.save(userId,surveyScore,facility);
    }

    @Override
    public String getFacilityStatus(Long userId) {
        Integer easy = resultMapper.getLocked("easy", userId);
        Integer medium = resultMapper.getLocked("medium", userId);
        Integer hard = resultMapper.getLocked("hard", userId);
        if ( easy==0&&medium==1&&hard==1){
            return "easy";
        }
        else if(easy==0&&medium==0&&hard==1){
            return "medium";
        }
        else  if(easy==0&&medium==0&&hard==0){
            return "hard";
        }
        return null;
    }
    @Override
    public Integer getSurveyScore(String sqlQuery) {
        return soulScoreMapper.getSoulScore(sqlQuery);
    }

    @Override
    public SoulTestResult getSoulTestConclusion(String reportScore) {
        return soulTestMapper.findConclusion(reportScore);
    }

    @Override
    public String findScoreType(String reportScore) {
        return soulScoreMapper.getScoreType(reportScore);
    }

    @Override
    public List<Dimensions> findDimensions(String scoreType) {
        return soulTestMapper.findDimensions(scoreType);
    }

    @Override
    public List<SimilarUser> findSimilarUser(String scoreType, Long userId) {
        return soulTestMapper.findSimilarUsers(scoreType, userId);
    }

    @Override
    public void saveReportResult(UserSoulResult userSoulResult) {
        resultMapper.insert(userSoulResult);
    }

    @Override
    public void saveUserScore(Long userId, Integer surveyScore, String facility) {
        soulScoreMapper.save(userId,surveyScore,facility);
    }

    @Override
    public String getFacilityStatus(Long userId) {
        Integer easy = resultMapper.getLocked("easy", userId);
        Integer medium = resultMapper.getLocked("medium", userId);
        Integer hard = resultMapper.getLocked("hard", userId);
        if ( easy==0&&medium==1&&hard==1){
            return "easy";
        }
        else if(easy==0&&medium==0&&hard==1){
            return "medium";
        }
        else  if(easy==0&&medium==0&&hard==0){
            return "hard";
        }
        return null;
    }

    @Override
    public void changeFacilityStatus(Long userId) {
        if (resultMapper.getLocked("medium", userId) == 1) {
            resultMapper.openLocked("medium", userId);
        } else {
            resultMapper.openLocked("hard", userId);
        }
    }

    @Override
    public UserSoulResult getUserSoulResult(Long userId) {
        return resultMapper.selectOne( new QueryWrapper<UserSoulResult>().lambda().eq(UserSoulResult::getUser_id, userId));
    }

    @Override
    public void updateUserSoulResult(Long userId, String scoreType) {
        resultMapper.updateUserSoulResult(userId, scoreType);
    }

    @Override
    public List<SoulTest> getSoulTests(Long userId) {
        List<SoulTest> soulTests = soulTestMapper.findAll();
        for (SoulTest soulTest : soulTests) {
            String id = soulTest.getId();
            List<SoulQuestion> soulQuestions = soulQuestionMapper.findBySid(id);
            for (SoulQuestion soulQuestion : soulQuestions) {
                List<Option> options = optionMapper.selectOptionByQid(soulQuestion.getId());
                soulQuestion.setOptions(options);
            }
            String level = soulTest.getLevel();
            Integer score=soulScoreMapper.findTestScore(userId,soulTest.getLevel());
            if(score!=null && soulTest.getLevel()!=null){
                soulTest.setReportId(score.toString()+soulTest.getLevel());
            }
            soulTest.setQuestions(soulQuestions);
            soulTest.setIsLock(resultMapper.getLocked(soulTestMapper.getFacility(id), userId));
        }
        return soulTests;
    }
}
